import Spring.demo.Main.StudentController1;
import Spring.demo.component.*;
import Spring.demo.controller.StudentController;
import Spring.demo.controller.User1Controller;
import Spring.demo.controller.UserController;
import Spring.demo.model.Student;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;

public class App {
    public static void main1(String[] args) {

        //1.获取Spring对象（上下文）
        //BeanFactory context = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

       ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        //2.取出bean对象
        //（1）根据Bean名称（标识）来得到Bean对象
        User user=(User) context.getBean("user");
        //User user1=(User) context.getBean("user1");
        //（2）根据Bean类型，来得到Bean对象（容易出错）
        //User user1=context.getBean(User.class);

        //（3）根据Bean名称（标识）+类型 来得到Bean对象
       // User user2=context.getBean("user",User.class);

        //3.使用bean
        System.out.println(user.hello());
        //System.out.println(user1.hello("hello"));
        //System.out.println(user2.hello("hello"));


    }


    public static void main2(String[] args) {
        //1.获取Spring对象（上下文）
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        //2.取出bean对象

        //（3）根据Bean名称（标识）+类型 来得到Bean
        ApplicController applicController =context.getBean("applicController", ApplicController.class);

        //3.使用bean
        System.out.println(applicController.hello());
    }

    public static void main3(String[] args) {
        //1.获取Spring对象（上下文）
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        //2.取出bean对象

        //（3）根据Bean名称（标识）+类型 来得到Bean
        aController controller  =context.getBean("aController", aController.class);

        //3.使用bean
        System.out.println(controller.hello());
    }

    public static void main5(String[] args) {
        //1.获取Spring对象（上下文）
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        //2.取出bean对象

        //（3）根据Bean名称（标识）+类型 来得到Bean
        BController controller  =context.getBean("BController", BController.class);

        //3.使用bean
        System.out.println(controller.hello());
    }


    public static void main6(String[] args) {
        //1.获取Spring对象（上下文）
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        //2.取出bean对象

        //（3）根据Bean名称（标识）+类型 来得到Bean
        UserComponent component =context.getBean("userComponent", UserComponent.class);

        //3.使用bean
        System.out.println(component.hello());
    }

    public static void main7(String[] args) {

        //1.获取Spring对象（上下文）
        BeanFactory context = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

//        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        //2.取出bean对象
        //（1）根据Bean名称（标识）来得到Bean对象
        User user=context.getBean("user",User.class);
        //User user1=(User) context.getBean("user1");
        //（2）根据Bean类型，来得到Bean对象（容易出错）
        //User user1=context.getBean(User.class);

        //（3）根据Bean名称（标识）+类型 来得到Bean对象
        // User user2=context.getBean("user",User.class);

        //3.使用bean
        System.out.println(user.hello());
        //System.out.println(user1.hello("hello"));
        //System.out.println(user2.hello("hello"));


    }

    public static void main8(String[] args) {

        //1.获取Spring对象（上下文）

        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
        //2.取出bean对象

        StudentController student =context.getBean("studentController",StudentController.class);
        student.hello();
    }


    public static void main9(String[] args) {

        //1.获取Spring对象（上下文）

        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
        //2.取出bean对象

        Student student =context.getBean("student1",Student.class);
        System.out.println(student);
        System.out.println("===========");
        Student stu =context.getBean("s1",Student.class);
        System.out.println(stu);
        System.out.println("===========");
        //Student stu1 =context.getBean("student2",Student.class);//这种是错误的
        //System.out.println(stu1);
    }

    public static void main10(String[] args) {

        //1.获取Spring对象（上下文）

        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
        //2.取出bean对象

       StudentController1 student =context.getBean("studentController1", StudentController1.class);
        student.fun();
    }

    public static void main11(String[] args) {

        //1.获取Spring对象（上下文）

        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
        //2.取出bean对象

        StudentController student =context.getBean("studentController",StudentController.class);
        student.hello();
    }

    public static void main12(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        UserController userController=context.getBean("userController",UserController.class);
        userController.getUser();
        User1Controller user1Controller=context.getBean("user1Controller",User1Controller.class);
        user1Controller.getUser();
    }

    public static void main(String[] args) {
        //1.获取Spring对象（上下文）
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");

        //2.取出bean对象

        //（3）根据Bean名称（标识）+类型 来得到Bean
        ApplicController applicController =context.getBean("applicController", ApplicController.class);

        //3.使用bean
        System.out.println(applicController.hello());
    }

}
